<?xml version="1.0" encoding="UTF-8"?><d:tdl xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/2006/btl"  xmlns:d="http://www.backbase.com/2006/tdl" >

	<d:namespace name="http://www.backbase.com/2006/btl">

		<d:uses element="positionElement dimensionElement containerElement" src="../visualElement/visualElement.xml"/>

		<d:element name="windowArea" extends="b:positionElement b:dimensionElement b:containerElement">
			
			<d:resource type="text/javascript"><![CDATA[btl.windowArea = {};

btl.windowArea.NEW_WINDOW_OFFSET = 15;

/*
 Adds a new window
 oController - windowArea
 oWindow - an element - window to be (it will be checked if it is a windowBase descendant)
 bSilent - don't activate the window

 returns: index of the window or -1 if it is not added
*/
btl.windowArea.addWindow = function btl_windowArea_addWindow( oController, oWindow, bInitial){
	//FIXME: this is to prevent adding same window twice due to the bug in the core when
	//"DOMNodeInsertedIntoDocument", this should be done some other more logical way
	if (oWindow._["_addedToArea"]) return;

	oWindow.addEventListener('open', function(event){ btl.windowArea.openWindowHandler(oController, event);}, false);
	oWindow.addEventListener('close', function( event){ btl.windowArea.openWindowHandler(oController, event)}, false);
	oWindow.setAttribute('dragConstraint', '..');
	oWindow.setAttribute('resizeConstraint', '..');
	//@TODO: place in right location:
	bb.html.removeClass( oWindow.viewNode, 'btl-window-active');

  	if (!oWindow.getProperty('open')){
		return -1;
  	} 
	this.sendEvent(oController, oWindow, 'add');

	if(!bInitial)
		oController.activate(oWindow);

	//FIXME: this is to prevent adding same window twice due to the bug in the core when
	//"DOMNodeInsertedIntoDocument", this should be done some other more logical way
	oWindow._["_addedToArea"] = true;
};

/*
 Removes a window
 oWindowArea - windowArea
 oWindow - a window to remove
*/
btl.windowArea.removeWindow = function btl_windowArea_removeWindow(oWindowArea, oWindow){
	var aWindows = oWindowArea.getProperty('windows');

	for(var i = aWindows.length - 1; i >= 0; i--){
		if(aWindows[i] == oWindow){
			if(oWindowArea._._activeWindow == oWindow)
				oWindowArea._._activeWindow = null;

			this.sendEvent(oWindowArea, oWindow, 'remove');
			break;
		}
	}
};

/*
	notifies window managers
*/
btl.windowArea.openWindowHandler = function btl_windowArea_openWindowHandler( oWindowArea, oEvent){
	// oWindowArea - windowArea
	// oEvent.target - window
	var oWindow	= oEvent.target;
	if( oEvent.type == 'open'){
		this.sendEvent( oWindowArea, oWindow, 'add');
		oWindowArea.activate(oWindow);
	} else {
		this.sendEvent( oWindowArea, oEvent.target, 'remove');
	}
};

/*
	notifyes window managers about window active state changing
*/
btl.windowArea.windowHandler = function btl_windowArea_windowHandler( oEvent){
	// oEvent.currentTarget - windowArea
	// oEvent.target - window
	var oWindowArea = oEvent.currentTarget;
	if( !oEvent.target || (oWindowArea == oEvent.target)) return;
	// find a window
	for(var oWindow = oEvent.target, oParent = oEvent.target.getProperty('parentNode'); oWindow && oParent != oWindowArea;
	    oParent = oParent.getProperty('parentNode')) {
		oWindow = oParent;
	}

	if( oWindow && (oWindowArea._._activeWindow != oWindow ) && bb.instanceOf( oWindow, btl.namespaceURI, 'windowBase')){
		oWindowArea.activate( oWindow);
	}
};

/*
 sends events to registered window managers
*/
btl.windowArea.sendEvent = function btl_windowArea_sendEvent( oController, oWindow, sType){
	var aWindowManagers = oController.getProperty('windowManagers');
	for(var i = aWindowManagers.length - 1; i >= 0; i--){
		var oEvent = bb.document.createEvent('Events');
		oEvent.initEvent( 'windowChanged' ,false, false);
		oEvent.sAction = sType;
		oEvent.oWindow = oWindow;
		oEvent.oWindowArea = oController;
		aWindowManagers[i].dispatchEvent(oEvent);
	}
};
/*
*/
btl.windowArea.isChildWindow = function btl_windowArea_isChildWindow( oController, oWindow){
	return (oController == oWindow.getProperty('parentNode')) && bb.instanceOf( oWindow, btl.namespaceURI, 'windowBase');
};
]]></d:resource>
			<d:resource type="text/css"><![CDATA[
				.btl-windowArea,
				.btl-windowArea-content {
					height: 100%;
					overflow: hidden;
					position: relative;
				}
				/* fixes */
				.btl-windowArea {
					box-sizing: border-box;
					-moz-box-sizing: border-box;
					-khtml-box-sizing: border-box;
				}
			]]></d:resource>

			<d:template type="application/xhtml+xml">
				<div class="btl-windowArea">
					<div class="btl-windowArea-content"><d:content/></div>
				</div>
			</d:template>

			<d:property name="windows">
				
				<d:getter type="text/javascript"><![CDATA[
					var aChildNodes = this.getProperty('childNodes');
					var aWindows = [];
					for(var i = 0, iMax = aChildNodes.length; iMax > i; i++){
						if(bb.instanceOf(aChildNodes[i], btl.namespaceURI, 'windowBase'))
							aWindows[aWindows.length] = aChildNodes[i];
					}
					return aWindows;
				]]></d:getter>
			</d:property>

			<d:property name="activeWindow">
				
			</d:property>

			<d:method name="activate">
				
				<d:argument name="window" required="true">
					
				</d:argument>
				<d:body type="text/javascript"><![CDATA[
					if(window) {
						window.setProperty('minimized', false);
					}

					if(this._['_activeWindow'] == window) {
						return;
					}

					if(this._['_activeWindow']) {
						this._['_activeWindow'].setAttribute('zIndex', this._._oldZIndex);
						bb.html.removeClass( this._['_activeWindow'].viewNode, 'btl-window-active');
					}

					this._['_oldZIndex'] = window.getAttribute('zIndex');

					window.setAttribute('zIndex', 800);
					this._['_activeWindow'] = window;
					bb.html.addClass( window.viewNode, 'btl-window-active');
					btl.windowArea.sendEvent( this, window, 'activate');
				]]></d:body>
			</d:method>

			<d:property name="windowManagers">
				
				<d:getter type="text/javascript"><![CDATA[
					if(!this._['_windowManagers'])
						this._['_windowManagers'] = [];
					return this._['_windowManagers'];
				]]></d:getter>
			</d:property>

			<d:method name="addWindowManager">
				

				<d:argument name="manager" required="true">
					
				</d:argument>
				<d:body type="text/javascript"><![CDATA[
					var aWindowManagers = this.getProperty('windowManagers');
					aWindowManagers[aWindowManagers.length] = manager;
					this.setProperty('windowManagers', aWindowManagers);
				]]></d:body>
			</d:method>

			<d:method name="removeWindowManager">
				
				<d:argument name="manager" required="true">
					
				</d:argument>
				<d:body type="text/javascript"><![CDATA[
					var aWindowManagers = this.getProperty('windowManagers');

					for(var i = aWindowManagers.length - 1; i >= 0; i--){
						if( aWindowManagers[i] == manager){
							aWindowManagers.splice(i, 1);
							this.setProperty('windowManagers', aWindowManagers);
							break;
						}
					}
				]]></d:body>
			</d:method>

			<d:method name="getWindow">
				
				<d:argument name="node">
					
				</d:argument>
				<d:body type="text/javascript"><![CDATA[
					if(!node)
						return null;
					var oStop = this.modelNode;
					for(var obj = node.modelNode; obj && obj != oStop && obj.nodeType == 1; obj = obj.parentNode){
						if( obj.parentNode == oStop){
							oController = bb.getControllerFromModel( obj);
							if( bb.instanceOf( oController, btl.namespaceURI, 'windowBase'))
								return oController;
						}
					}
					return null;
				]]></d:body>
			</d:method>

			<d:handler event="resizeEnd" type="text/javascript"><![CDATA[
				if( btl.windowArea.isChildWindow(this, event.target)) {
					btl.windowArea.sendEvent(event.currentTarget, event.target, 'change');
				}
			]]></d:handler>

			<d:handler event="mousedown" type="text/javascript"><![CDATA[
				btl.windowArea.windowHandler( event);
			]]></d:handler>

			<d:handler event="DOMNodeInsertedIntoDocument" type="text/javascript"><![CDATA[
				this._['_iStartLeft'] = 0;
				this._['_iStartTop'] = 0;

				var aWindows = this.getProperty('windows');

				for(var i = 0, iMax = aWindows.length; iMax > i ; i++){
					btl.windowArea.addWindow(this, aWindows[i], true);
				}
				// activate the last window that is not minimized
				for (var i = aWindows.length - 1; i >= 0; i--) {
					if (!aWindows[i].getProperty('minimized')) {
						this.activate(aWindows[i]);
						break;
					}
				}
			]]></d:handler>

			<d:handler event="DOMNodeInserted" type="text/javascript"><![CDATA[
				var oTarget = event.target;
				if( event.relatedNode == this && bb.instanceOf(oTarget, btl.namespaceURI, 'windowBase')){
					btl.windowArea.addWindow(this, oTarget);
				}
			]]></d:handler>

			<d:handler event="DOMNodeRemoved" type="text/javascript"><![CDATA[
				var oTarget = event.target;
				if(event.relatedNode == this && bb.instanceOf(oTarget, btl.namespaceURI, 'windowBase')) { // only for children that are window
					btl.windowArea.removeWindow(this, oTarget);
				}
			]]></d:handler>
		</d:element>
	</d:namespace>
</d:tdl>